<template>
  <div style="border: 1px solid #ccc">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef"
        :defaultConfig="toolbarConfig"
        :mode="mode"
      />
      <Editor
        style="height: 500px; overflow-y: hidden;"
        v-model="valueHtml"
        :defaultConfig="editorConfig"
        :mode="mode"
        @onCreated="handleCreated"
        @onChange="handleChange"
      />
    </div>
</template>

<script>
import '@wangeditor/editor/dist/css/style.css'
import { onBeforeUnmount, ref, shallowRef, onMounted } from 'vue';
import { Editor, Toolbar } from '@wangeditor/editor-for-vue';

export default {
    components: { Editor, Toolbar },
    setup() {
        //编辑器实例,必须用shallowRef
        const editorRef = shallowRef()
        //内容HTML
        const valueHtml = ref('<p></p>')
        onMounted(() => {
            setTimeout(() => {
                valueHtml.value='<span></span>'
            },1500)
        })
        const toolbarConfig = {}
        const editorConfig = { placeholder: '内容...' }
        //页面销毁时清除编辑器
        onBeforeUnmount(() => {
            const editor = editorRef.value
            if (editor == null) return
            editor.destory()
        })
        const handleCreated = (editor) => {
            editorRef.value = editor
            console.log(editorRef);
        }
        const handleChange = (editor) => {
            console.log(editor.children);
        }
        return {
            editorRef,
            valueHtml,
            mode: 'simple',
            toolbarConfig,
            editorConfig,
            handleCreated,
            handleChange
        }
    }
}
</script>

<style scoped>
</style>
